<template>
  <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

    <div class="title-container">
      <h3 class="title">短信登陆</h3>
    </div>

    <div>
      <el-form-item prop="phone" class="phone">
        <span class="svg-container">
          <i class="el-icon-phone" />
        </span>
        <el-input
          ref="phone"
          v-model="loginForm.phone"
          style="width:200px"
          placeholder="手机号"
          name="phone"
          tabindex="1"
          auto-complete="on"
          minlength="11"
          maxlength="11"
        />
        <el-button class="send-buttun" type="warning" style="float:right" :disabled="smsSendIng" @click="sendcode">
          <div v-if="smsSendIng">{{ waitTime }}秒后可在再次发送</div>
          <div v-else>发送验证码</div>
        </el-button>
      </el-form-item>

    </div>

    <el-form-item prop="code">
      <span class="svg-container">
        <svg-icon icon-class="password" />
      </span>
      <el-input
        ref="code"
        v-model="loginForm.code"
        placeholder="验证码"
        name="code"
        tabindex="2"
        :minlength="smsCodeLength"
        :maxlength="smsCodeLength"
        @keyup.enter.native="handleLogin"
      />
    </el-form-item>
    <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登陆</el-button>

  </el-form>
</template>

<script>
import { validPhone } from '@/utils/validate'
import { sendSmsCode } from '@/api/public'
export default {
  name: 'Login',
  data() {
    const validatePhone = (rule, value, callback) => {
      if (!validPhone(value)) {
        callback(new Error('请输入正确的手机号'))
      } else {
        callback()
      }
    }
    const validateCode = (rule, value, callback) => {
      if (value.length !== this.smsCodeLength) {
        callback(new Error('验证码必须是' + this.smsCodeLength + '位数字'))
      } else {
        callback()
      }
    }
    return {
      intervalBtn: {},
      smsSendIng: false,
      smsCodeLength: 6,
      waitTime: 120,
      loginForm: {
        phone: '',
        code: ''
      },
      loginRules: {
        phone: [{ required: true, trigger: 'blur', validator: validatePhone }],
        code: [{ required: true, trigger: 'blur', validator: validateCode }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  computed: {
    getCodeBtnDisable: {
      get() {
        if (this.waitTime === 120) {
          if (this.loginForm.phone) {
            return false
          }
          return true
        }
        return true
      },
      set() {}
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    timerStart() {
      this.intervalBtn = setInterval(() => {
        if (this.waitTime <= 0) {
          clearInterval(this.intervalBtn)
          this.smsSendIng = !this.smsSendIng
          this.waitTime = 120
        }
        this.waitTime--
      }, 1000)
    },
    changeSendState() {
      this.smsSendIng = !this.smsSendIng
    },
    sendcode() {
      if (!validPhone(this.loginForm.phone)) {
        this.$message.error('请输入合法手机号')
        return
      }
      sendSmsCode(this.loginForm.phone)
      this.loginForm.code = ''
      this.changeSendState()
      this.timerStart()
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  .el-button{
    margin: 5px;
  }
  .el-input {
    display: inline-block;
    height: 47px;

    input {
      width: 100%;
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      // padding: 12px 5px 12px 15px;
      height: 47px;

    }
  }

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }

}
</style>
